import datastructure.linklist.ListNode;
import top100.twopointer.Solution287;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/28 14:18
 * @description:
 */
public class Solution876 {
    public ListNode middleNode(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode slow = head;
        ListNode fast = dummy;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        Solution876 solution = new Solution876();
        ListNode head = ListNode.arrayToList(new int[]{1, 2, 3, 4, 5});//3
        ListNode res = solution.middleNode(head);
        System.out.println(res.val);
        head = ListNode.arrayToList(new int[]{1, 2, 3, 4, 5, 6});//4
        res = solution.middleNode(head);
        System.out.println(res.val);
    }
}
